function draw_triangle(x, y, width, height, screen, color) {
    var left = x - width/2;
    var right = x + width/2;
    var top = y - height/2;
    var bottom = y + height/2;
    var middle = x;
    var graph = screen.getContext("2d");
    graph.strokeStyle = color;
    graph.beginPath();
    graph.moveTo(left, bottom);
    graph.lineTo(middle, top);
    graph.stroke();
    graph.moveTo(middle, top);
    graph.lineTo(right, bottom);
    graph.stroke();
    graph.moveTo(right, bottom);
    graph.lineTo(left, bottom);
    graph.stroke();
}

function draw_rect(x, y, width, height, screen, color, filled) {
    var graph = screen.getContext("2d");
    if (filled) { 
        graph.fillStyle = color;
        graph.fillRect(x, y, width, height);
    }
    else {
        graph.strokeStyle = color;
        graph.strokeRect(x, y, width, height);
    }
}

function draw_line(x,y, x1,x2, screen, color) {
    var graph = screen.getContext("2d");
    graph.strokeStyle = color;
    graph.beginPath();
    graph.moveTo(x,y);
    graph.lineTo(x1,x2);
    graph.stroke();
}

function draw_circle(x, y, radius, screen, color, filled, opacity) {
    var graph = screen.getContext("2d");
    graph.beginPath();
    graph.arc(x, y, radius, 0, 2*Math.PI);
    if (filled) {
        graph.fillStyle = "rgba(" + color + ", " + opacity + ')';
        graph.fillStyle = color;
        graph.fill();
    }
    else {
        graph.strokeStyle = color;
        graph.stroke();
    }
}

function draw_text(x, y, text, screen, font, color, filled, opacity) {
    var graph = screen.getContext("2d");
    graph.beginPath();
    graph.font = font;
    var font_size = /(\d+)(em|pt|px)/;
    var size = parseFloat(font_size.exec(font)[1]);
    if (filled) {
        graph.fillStyle = "rgba(" + color + ", " + opacity + ')';
        graph.fillStyle = color;
        graph.fillText(text, x, y + size);
    }
    else {
        graph.strokeStyle = color;
        graph.strokeText(text, x, y + size);
    }
}

function strip(string, bad_chars) {
    var left_index = 0;
    var right_index = string.length -1;
    for (left_index = 0; left_index < string.length; left_index = left_index + 1) {
        if (bad_chars.indexOf(string.charAt(left_index)) === -1) {
            break;
        }
    }
    for (right_index = string.length - 1; right_index > -1; right_index = right_index - 1) {
        if (bad_chars.indexOf(string.charAt(right_index)) === -1) {
            break;
        }
    }
    return string.substring(left_index, right_index);
}